
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for _default.
	''' </summary>
	Public Partial Class login
		Inherits BaseWebPage

		Protected Label1 As System.Web.UI.WebControls.Label
		Protected Label2 As System.Web.UI.WebControls.Label
		Protected txtEmailRemind As System.Web.UI.WebControls.TextBox
		Protected btnRemind As System.Web.UI.WebControls.ImageButton
		Protected revEmailRemind As System.Web.UI.WebControls.RegularExpressionValidator
		Protected rvEmailRemind As System.Web.UI.WebControls.RequiredFieldValidator
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.LOGIN_PAGE;
			ProcessActions()
			Dim isLogoutClicked As String = Request.Params("logout")

			Me.initPageTitles(Constants.LOGIN_PAGE, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			btnLogin.Attributes.Add("onMouseOver", "this.src='images/login_over.gif'")
			btnLogin.Attributes.Add("onMouseOut", "this.src='images/login.gif'")

			If isLogoutClicked = "1" Then
				LoggedUser = Nothing
				Common.Utils.RedirectToLoginPage(Page)
			Else
				If Request.Cookies(Constants.cookiePassword) <> Nothing AndAlso Request.Cookies(Constants.cookieEmail) <> Nothing Then
					Dim cookieEmail As String = Request.Cookies(Constants.cookieEmail).Value
					Dim cookiePass As String = Request.Cookies(Constants.cookiePassword).Value
					Dim usr As User = Nothing
					usr = Users.Login(cookieEmail, cookiePass)

					If usr <> Nothing Then
						' upon success save the user ID in the session and redirect to his home page
						usr.Save()
						LoggedUser = usr

						Response.Redirect(Constants.WELCOME)
					End If
				End If
			End If

			Dim strScript As String = ""
			strScript = "<script language=javascript> document.all('" + txtEmail.UniqueID + "').focus() </script>"
			Me.RegisterStartupScript("focus", strScript)
			If (Request.ServerVariables("HTTP_REFERER") <> Nothing) AndAlso (Request.ServerVariables("HTTP_REFERER").ToString().IndexOf(Constants.[DEFAULT]) <> -1) Then
				If Not Page.IsPostBack Then
					If Request.QueryString("usr") <> Nothing Then
						Me.txtEmail.Text = Request.QueryString("usr").ToString()

						'Page.Validate();

						'if (Page.IsValid && (Session["usr_pass"] != null))
						If Session("usr_pass") <> Nothing Then
							Me.pswPassword.Text = Session("usr_pass").ToString()
						End If

						Session("usr_pass") = Nothing

						Page.Validate()
					End If
				End If
			End If

			'get url of the failed login (the page that redirected to the login page)
			If Not Page.IsPostBack Then
				If Request.QueryString("ref") <> Nothing AndAlso Request.QueryString("ref").ToString() <> "" Then
					Dim sReferer As String = Request.QueryString.ToString()
					If sReferer.IndexOf("ref") > -1 Then
						sReferer = sReferer.Substring(sReferer.IndexOf("ref=") + 4)

						sReferer = sReferer.Replace(".aspx&", ".aspx?")

						Me.hdnReferer.Value = sReferer
					End If
				Else
					Me.hdnReferer.Value = ""
				End If
			End If

		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvWrongDetails.ServerValidate, AddressOf Me.cvWrongDetails_ServerValidate
			AddHandler Me.btnLogin.Click, AddressOf Me.btnLogin_Click

		End Sub
#End Region


		Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
			If Page.IsValid Then
				If Request.Params(chkRemember.UniqueID) = "on" Then
					Response.Cookies(Constants.cookieEmail).Expires = DateTime.MaxValue
					Response.Cookies(Constants.cookiePassword).Expires = DateTime.MaxValue
					Response.Cookies(Constants.cookieEmail).Value = txtEmail.Text.Trim()
					Response.Cookies(Constants.cookiePassword).Value = pswPassword.Text.Trim()
				End If

				Dim usr As User = Users.Login(txtEmail.Text, pswPassword.Text)

				If usr <> Nothing Then
					' upon success save the user ID in the session and redirect to his home page
					usr.Save()
					LoggedUser = usr

					If Me.hdnReferer.Value <> "" Then
						Response.Redirect(Me.hdnReferer.Value)
					Else
						Response.Redirect(Constants.WELCOME)
					End If
				End If
			End If
		End Sub
		Private Sub ProcessActions()
			If Constants.REGISTRATION_CONFIRM_PARAM_ACTION_VALUE.Equals(Request.Params(Constants.REGISTRATION_CONFIRM_PARAM_ACTION)) Then
				' Registration confirmation
				Dim userEmail As String = Request(Constants.LOGIN_PARAM_USER)
				Dim userPassHash As String = Request(Constants.LOGIN_PARAM_KEY)

				ConfirmUserRegistration(userEmail, userPassHash)
			End If
		End Sub

		Private Sub ConfirmUserRegistration(ByVal userEmail As String, ByVal passHash As String)
			Dim user As BO.User = BO.Users.LoginWithPassHash(userEmail, passHash)
			If user <> Nothing AndAlso user.Status = BO.Users.STATUS_UNCONFIRMED Then
				user.SetActive()
				user.Save()
				LoggedUser = user
			End If
		End Sub

		Private Sub cvWrongDetails_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			If txtEmail.Text.Trim() <> "" AndAlso pswPassword.Text.Trim() <> "" Then
				Dim usr As User = Users.Login(txtEmail.Text, pswPassword.Text)
				If usr = Nothing Then
					args.IsValid = False
				Else
					args.IsValid = True
				End If
			End If
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






